/*************************************************************************
 *                                                                       *
 * This file is part of the FreeMAPs distribution.  For full copyright   *
 * information, see COPYRIGHT and COPYING.LESSER.                        *
 *                                                                       *
 * Author:           sqbang                                              *
 * Email:            sqbang@ustc.edu                                     *
 * Date:             2015.07.10                                          *
 * Description:      SimpleRegion that has a unitary material            *
 *                                                                       *
 *************************************************************************/

//declaration header
#include "COMMON/regn/SimpleRegion.h"

//COMMON headers
#include "COMMON/info/InfoManager.h"
#include "COMMON/info/AttributeInfo.h"
#include "COMMON/tbox/LogMessage.h"


namespace FreeMAPs
{

SimpleRegion::SimpleRegion (
    const std::string& parent_name,
    const std::string& region_name,
    const bool use_structured_mesh):
    Parent (parent_name, region_name, false),
    d_use_structured_mesh (use_structured_mesh),
    d_attribute (0)
{}



SimpleRegion::~SimpleRegion()
{}



void
SimpleRegion::get_from_input (
    const InputDatabase* db)
{
    this->prepare_for_use (db);

    db->insert_allowable_key ("attribute_name");

    db->check_allowable_keys();

    /*
     * build attribute_name
     */
    {
        const AttributeInfo* attr_info = InfoManager::get_instance()->get_attribute_info();

        FREEMAPS_ASSERT_VALID_POINTER (attr_info);
        db->check_optional_key ("attribute_name", DataType::STRING, false, false);
        if (db->key_exists ("attribute_name") && db->is_string("attribute_name"))
        {
            const std::string& attr_name = db->get_string ("attribute_name");
            if (!attr_info->has_attribute (attr_name))
            {
                LogMessage::use_improper_string_value (
                    db,
                    "attribute_name",
                    attr_name,
                    "it should be an attribute name set in attribute_info.",
                    Utilities::get_map_keys (attr_info->get_attributes()));
            }
            else
            {
                d_attribute = attr_info->get_attribute (attr_name);
            }
        }
        else
        {
            d_attribute = attr_info->get_default_attribute();
        }
    }
}



std::ostream&
operator << (
    std::ostream& os,
    const SimpleRegion& rhs)
{
    os << rhs.get_simple_region_name() << "\n{\n";

    if (rhs.d_attribute)
    {
        os << LogMessage::get_prefix (ONE_TAB) << "attribute_name = \"" << rhs.d_attribute->get_attribute_name() << "\"\n";
    }

    os << "}";

    return os;
}

}
